/**
 *
 */
package org.jeecg.client.ws;

import org.jeecg.client.BizRequest;
import org.jeecg.client.BizResponse;
import org.jeecg.client.DataSet;
import org.jeecg.client.IBizCommand;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author bright
 *
 */
public class WCFClient implements IBizCommand {
	private String name;
	private String url;
	private IWCFService service;
	public WCFClient(String name, String args) throws MalformedURLException{
		this.name = name;
		String[] _args = args.split(":");
		this.url=String.format("http://%s:%s/", _args[1], _args[2]);
	}

	protected void checkService() throws MalformedURLException{
		synchronized (this) {
			if (service == null)
				service = new WCFListener(new URL(this.url)).getBasicHttpBindingIWCFService();
		}
	}

	/* (non-Javadoc)
	 * @see org.jeecg.client.IBizCommand#RunCmd(org.jeecg.client.BizRequest)
	 */
	@Override
	public BizResponse RunCmd(BizRequest request) {
		try{
			checkService();
			String respStr = service.runCmd(request.getCmdName(), request.toString());
			return BizResponse.Parse(respStr);
		}catch(Exception ex){
			return new BizResponse(BizResponse.ServerUnreachable, ex.getMessage());
		}
	}

	@Override
	public DataSet RunDbCmd(BizRequest request) throws Exception {
		checkService();
		String respStr = service.runDbCmd(request.getCmdName(), request.toString());
		return DataSet.Parse(respStr);
	}
}
